package calculator;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;


public class InfixToSuffix {
    /**
     * @Description: 中缀表达式转后缀
     * @Param: [s]
     * @return: java.lang.String
     */
    public String infixToSuffix(String s) {
        Stack<Character> ope_stack = new Stack<>();
        Stack<Character> num_stack = new Stack<>();
        int i = 0;
        while (i < s.length()) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {
                num_stack.push(c);

            } else {
//                num_stack.push(' ');
                if (!ope_stack.isEmpty()) {
                    if (getPriority(ope_stack.peek()) >= getPriority(c)) {
                        while (!ope_stack.isEmpty() && getPriority(ope_stack.peek()) >= getPriority(c)) {
                            num_stack.push(ope_stack.pop());
//                            加空格为了区分数字
//                            num_stack.push(' ');
                        }
                    }
                }
                ope_stack.push(c);
            }
            i++;
        }
        //将运算符栈放到操作数栈
        while (!ope_stack.isEmpty()) {
            num_stack.push(ope_stack.pop());
//            num_stack.push(' ');
        }
        StringBuilder sb = new StringBuilder();
        while (!num_stack.isEmpty()) {
            sb.append(num_stack.pop());
        }
        //倒序输出
        return sb.reverse().toString();
    }


    public int getPriority(char c) {
        switch (c) {
            case '+':
            case '-': {
                return 1;
            }
            case '*':
            case '/': {
                return 2;
            }
        }
        return -1;
    }

    /**
     * @Description: 将字符串，拆分
     * @Param: [str]
     * @return: java.util.List<java.lang.String>
     */
    public List<String> stringToList(String str) {
        ArrayList<String> list = new ArrayList<>();
        char[] chars = str.toCharArray();
        int i = 0;
        while (i < chars.length) {
            if (Character.isDigit(chars[i])) {
                int num = 0;
                while (i < chars.length && Character.isDigit(chars[i])) {
                    num = num * 10 + chars[i] - '0';
                    i++;
                }
                list.add(num + "");
            } else {
                list.add(chars[i] + "");
                i++;
            }
        }
        return list;
    }

    public int infixCal(List<String> list) {
        Stack<Integer> stack = new Stack<>();
        String preOperator = "+";
        int num = 0;
        for (int i = 0; i < list.size(); i++) {
            String s1 = list.get(i);
            if (isNum(s1)) {
                num = Integer.parseInt(s1);
            }
            //1 2
            if (!isNum(s1) || i == list.size() - 1) {
                switch (preOperator) {//这里是前一个操作符
                    case "+": {
                        stack.push(num);
                        break;
                    }
                    case "-": {
                        stack.push(-num);
                        break;
                    }
                    case "*": {
                        stack.push(num * stack.pop());
                        break;
                    }
                    case "/": {
                        stack.push(stack.pop() / num);
                        break;
                    }
                }
                preOperator = s1;
            }
        }
        //全部相加
        int ans = 0;
        while (!stack.isEmpty()) {
            ans += stack.pop();
        }
        return ans;
    }

    public boolean isNum(String s) {
        return !s.equals("+") && !s.equals("-") && !s.equals("*") && !s.equals("/");
    }

    public static void main(String[] args) {
        InfixToSuffix obj = new InfixToSuffix();
        String s = obj.infixToSuffix("11/51+21*13-41*41");
        System.out.println(s);
// String s2 = obj.infixToSuffix2("11*(5+21)/13-41*4");
//        System.out.println(11*(5+21)/13-41*4);
//        System.out.println(s2);

//        String str = "2*3";
//        List<String> list = obj.stringToList(str);
//        System.out.println(list);
//        int i = obj.infixCal(list);
//        System.out.println(i);

    }
}
